﻿using HaoCoding.DCdb.WaterfallTree;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace HaoCoding.DCdb.Database
{
    /// <summary>
    /// 存储引擎
    /// </summary>
    public class StorageEngine : WTree, IStorageEngine
    {
        // 用户 数据格式
        private Dictionary<string, Item1> map = new Dictionary<string, Item1>();

        /// <summary>
        /// 同步锁
        /// </summary>
        private readonly object SyncRoot = new object();

        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="heap"></param>
        public StorageEngine(IHeap heap)
            : base(heap)
        {
            foreach (var locator in GetAllLocators())
            {
                if (locator.IsDeleted)
                    continue;

                Item1 item = new Item1(locator, null);

                map[locator.Name] = item;
            }
        }

        #region IStorageEngine

        public IEnumerator<IDescriptor> GetEnumerator()
        {
            lock (SyncRoot)
            {
                return map.Select(x => (IDescriptor)x.Value.Locator).GetEnumerator();
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        private class Item1
        {
            /// <summary>
            /// 数据定位
            /// </summary>
            public Locator Locator;

            /// <summary>
            /// 表
            /// </summary>
            public XTablePortable Table;

            public Item1(Locator locator, XTablePortable table)
            {
                Locator = locator;
                Table = table;
            }
        }

    }
}
